home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / machine / eeprom.c < prev    next >
C/C++ Source or Header  |  2000-05-01  |  6KB  |  235 lines

  1. #include "driver.h"
  2. #include "eeprom.h"
  3.  
  4. #define VERBOSE 0
  5.  
  6. #define SERIAL_BUFFER_LENGTH 40
  7.  
  8. static struct EEPROM_interface *intf;
  9.  
  10. static int serial_count;
  11. static char serial_buffer[SERIAL_BUFFER_LENGTH];
  12. static unsigned char eeprom_data[256];
  13. static int eeprom_data_bits;
  14. static int eeprom_read_address;
  15. static int eeprom_clock_count;
  16. static int latch,reset_line,clock_line,sending;
  17. static int locked;
  18.  
  19.  
  20. void EEPROM_init(struct EEPROM_interface *interface)
  21. {
  22.     intf = interface;
  23.  
  24.     memset(eeprom_data,0xff,(1 << intf->address_bits) * intf->data_bits / 8);
  25.     serial_count = 0;
  26.     latch = 0;
  27.     reset_line = ASSERT_LINE;
  28.     clock_line = ASSERT_LINE;
  29.     sending = 0;
  30.     if (intf->cmd_unlock) locked = 1;
  31.     else locked = 0;
  32. }
  33.  
  34. static void EEPROM_write(int bit)
  35. {
  36. #if VERBOSE
  37. logerror("EEPROM write bit %d\n",bit);
  38. #endif
  39.  
  40.     if (serial_count >= SERIAL_BUFFER_LENGTH-1)
  41.     {
  42. logerror("error: EEPROM serial buffer overflow\n");
  43.         return;
  44.     }
  45.  
  46.     serial_buffer[serial_count++] = (bit ? '1' : '0');
  47.     serial_buffer[serial_count] = 0;    /* nul terminate so we can treat it as a string */
  48.  
  49.     if (intf->cmd_read && serial_count == (strlen(intf->cmd_read) + intf->address_bits) &&
  50.             !strncmp(serial_buffer,intf->cmd_read,strlen(intf->cmd_read)))
  51.     {
  52.         int i,address;
  53.  
  54.         address = 0;
  55.         for (i = 0;i < intf->address_bits;i++)
  56.         {
  57.             address <<= 1;
  58.             if (serial_buffer[i + strlen(intf->cmd_read)] == '1') address |= 1;
  59.         }
  60.         if (intf->data_bits == 16)
  61.             eeprom_data_bits = (eeprom_data[2*address+0] << 8) + eeprom_data[2*address+1];
  62.         else
  63.             eeprom_data_bits = eeprom_data[address];
  64.         eeprom_read_address = address;
  65.         eeprom_clock_count = 0;
  66.         sending = 1;
  67.         serial_count = 0;
  68. logerror("EEPROM read %04x from address %02x\n",eeprom_data_bits,address);
  69.     }
  70.     else if (intf->cmd_erase && serial_count == (strlen(intf->cmd_erase) + intf->address_bits) &&
  71.             !strncmp(serial_buffer,intf->cmd_erase,strlen(intf->cmd_erase)))
  72.     {
  73.         int i,address;
  74.  
  75.         address = 0;
  76.         for (i = 0;i < intf->address_bits;i++)
  77.         {
  78.             address <<= 1;
  79.             if (serial_buffer[i + strlen(intf->cmd_erase)] == '1') address |= 1;
  80.         }
  81. logerror("EEPROM erase address %02x\n",address);
  82.         if (locked == 0)
  83.         {
  84.             if (intf->data_bits == 16)
  85.             {
  86.                 eeprom_data[2*address+0] = 0x00;
  87.                 eeprom_data[2*address+1] = 0x00;
  88.             }
  89.             else
  90.                 eeprom_data[address] = 0x00;
  91.         }
  92.         else
  93. logerror("Error: EEPROM is locked\n");
  94.         serial_count = 0;
  95.     }
  96.     else if (intf->cmd_write && serial_count == (strlen(intf->cmd_write) + intf->address_bits + intf->data_bits) &&
  97.             !strncmp(serial_buffer,intf->cmd_write,strlen(intf->cmd_write)))
  98.     {
  99.         int i,address,data;
  100.  
  101.         address = 0;
  102.         for (i = 0;i < intf->address_bits;i++)
  103.         {
  104.             address <<= 1;
  105.             if (serial_buffer[i + strlen(intf->cmd_write)] == '1') address |= 1;
  106.         }
  107.         data = 0;
  108.         for (i = 0;i < intf->data_bits;i++)
  109.         {
  110.             data <<= 1;
  111.             if (serial_buffer[i + strlen(intf->cmd_write) + intf->address_bits] == '1') data |= 1;
  112.         }
  113. logerror("EEPROM write %04x to address %02x\n",data,address);
  114.         if (locked == 0)
  115.         {
  116.             if (intf->data_bits == 16)
  117.             {
  118.                 eeprom_data[2*address+0] = data >> 8;
  119.                 eeprom_data[2*address+1] = data & 0xff;
  120.             }
  121.             else
  122.                 eeprom_data[address] = data;
  123.         }
  124.         else
  125. logerror("Error: EEPROM is locked\n");
  126.         serial_count = 0;
  127.     }
  128.     else if (intf->cmd_lock && serial_count == strlen(intf->cmd_lock) &&
  129.             !strncmp(serial_buffer,intf->cmd_lock,strlen(intf->cmd_lock)))
  130.     {
  131. logerror("EEPROM lock\n");
  132.         locked = 1;
  133.         serial_count = 0;
  134.     }
  135.     else if (intf->cmd_unlock && serial_count == strlen(intf->cmd_unlock) &&
  136.             !strncmp(serial_buffer,intf->cmd_unlock,strlen(intf->cmd_unlock)))
  137.     {
  138. logerror("EEPROM unlock\n");
  139.         locked = 0;
  140.         serial_count = 0;
  141.     }
  142. }
  143.  
  144. static void EEPROM_reset(void)
  145. {
  146. if (serial_count)
  147.     logerror("EEPROM reset, buffer = %s\n",serial_buffer);
  148.  
  149.     serial_count = 0;
  150.     sending = 0;
  151. }
  152.  
  153.  
  154. void EEPROM_write_bit(int bit)
  155. {
  156. #if VERBOSE
  157. logerror("write bit %d\n",bit);
  158. #endif
  159.     latch = bit;
  160. }
  161.  
  162. int EEPROM_read_bit(void)
  163. {
  164.     int res;
  165.  
  166.     if (sending)
  167.         res = (eeprom_data_bits >> intf->data_bits) & 1;
  168.     else res = 1;
  169.  
  170. #if VERBOSE
  171. logerror("read bit %d\n",res);
  172. #endif
  173.  
  174.     return res;
  175. }
  176.  
  177. void EEPROM_set_cs_line(int state)
  178. {
  179. #if VERBOSE
  180. logerror("set reset line %d\n",state);
  181. #endif
  182.     reset_line = state;
  183.  
  184.     if (reset_line != CLEAR_LINE)
  185.         EEPROM_reset();
  186. }
  187.  
  188. void EEPROM_set_clock_line(int state)
  189. {
  190. #if VERBOSE
  191. logerror("set clock line %d\n",state);
  192. #endif
  193.     if (state == PULSE_LINE || (clock_line == CLEAR_LINE && state != CLEAR_LINE))
  194.     {
  195.         if (reset_line == CLEAR_LINE)
  196.         {
  197.             if (sending)
  198.             {
  199.                 if (eeprom_clock_count == intf->data_bits && intf->enable_multi_read)
  200.                 {
  201.                     eeprom_read_address = (eeprom_read_address + 1) & ((1 << intf->address_bits) - 1);
  202.                     if (intf->data_bits == 16)
  203.                         eeprom_data_bits = (eeprom_data[2*eeprom_read_address+0] << 8) + eeprom_data[2*eeprom_read_address+1];
  204.                     else
  205.                         eeprom_data_bits = eeprom_data[eeprom_read_address];
  206.                     eeprom_clock_count = 0;
  207. logerror("EEPROM read %04x from address %02x\n",eeprom_data_bits,eeprom_read_address);
  208.                 }
  209.                 eeprom_data_bits = (eeprom_data_bits << 1) | 1;
  210.                 eeprom_clock_count++;
  211.             }
  212.             else
  213.                 EEPROM_write(latch);
  214.         }
  215.     }
  216.  
  217.     clock_line = state;
  218. }
  219.  
  220.  
  221. void EEPROM_load(void *f)
  222. {
  223.     osd_fread(f,eeprom_data,(1 << intf->address_bits) * intf->data_bits / 8);
  224. }
  225.  
  226. void EEPROM_save(void *f)
  227. {
  228.     osd_fwrite(f,eeprom_data,(1 << intf->address_bits) * intf->data_bits / 8);
  229. }
  230.  
  231. void EEPROM_set_data(UINT8 *data, int length)
  232. {
  233.     memcpy(eeprom_data, data, length);
  234. }
  235.